package com.zzz.robot201.mvp.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zzz.robot201.R;
import com.zzz.robot201.app.base.BaseViewActivity;
import com.zzz.robot201.app.config.AppContent;
import com.zzz.robot201.utils.LogUtils;
import com.zzz.robot201.utils.PermissionUtil;
import com.zzz.robot201.utils.SpUtils;

import java.util.List;

public class SplashActivity extends BaseViewActivity {

    private final int GO_MAIN = 9;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GO_MAIN:
                    SharedPreferences sharedPreferences = getSharedPreferences("share", MODE_PRIVATE);
                    boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    if (isFirstRun) {
                        editor.putBoolean("isFirstRun", false);
                        editor.commit();
                        SpUtils.put(SplashActivity.this, AppContent.SaveInfoKey.ISPHONE, true);
                    }
//                    String token = (String) SpUtils.get(SplashActivity.this, AppContent.SaveInfoKey.TOKEN, "");
//                    if (!TextUtils.isEmpty(token)){
                    startActivity(new Intent(SplashActivity.this, EquipmentActivity.class));
//                    }else {
//                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
//                    }
                    finish();
                    break;
            }
        }
    };


    @Override
    protected boolean useLandscape() {
        return false;
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_splash;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        applyPermissions();
    }

    public void applyPermissions() {
        PermissionUtil.applyPermissions(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
                //全部权限通过的回调
                permissionsSuccess();
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {
                //某一个权限拒绝的回调
                LogUtils.logGGQ("onRequestPermissionFailure");
                for (String permission : permissions) {
                    LogUtils.logGGQ(permission + "被拒绝了");
                }
            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {
                //拒绝后点击不再询问的回调
                LogUtils.logGGQ("onRequestPermissionFailureWithAskNeverAgain");
                for (String permission : permissions) {
                    LogUtils.logGGQ(permission + "不在询问");
                }
            }
        }, this.getRxPermissions(), ArmsUtils.obtainAppComponentFromContext(this).rxErrorHandler());
    }


    public RxPermissions getRxPermissions() {
        return new RxPermissions(this);
    }


    public void permissionsSuccess() {
        handler.sendEmptyMessageDelayed(GO_MAIN, 3000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (handler != null)
            handler.removeCallbacksAndMessages(null);

    }

}